package sedev.struts.portlet;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.interceptor.ServletRequestAware;

import sedev.shopping.business.SanPhamService;
import sedev.shopping.entities.ListSanPham;
import sedev.shopping.entities.SanPham;
import sedev.shopping.util.ConfigUtil;
import sedev.shopping.util.Paging;
import sedev.shopping.util.SedevUtil;

import com.opensymphony.xwork2.ActionSupport;

public class GuestAction extends ActionSupport implements ServletRequestAware {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private HttpServletRequest request;	
	@Override
	public void setServletRequest(HttpServletRequest request) {
		// TODO Auto-generated method stub
		this.request = request;		
	}
	
	@Override
	public String execute() throws Exception {	
		Paging paging = new Paging();
		paging.setPageSize(Integer.valueOf(ConfigUtil.getProperties().getProperty("guest.pagesize", "10")));
		ListSanPham listvip = null;
		ListSanPham listnewest = null;
		long ownerShopId = SedevUtil.getOwnerShopId(this.request);
		if(ownerShopId >0){
			// get data of user
			listvip = SanPhamService.getListDataVipByUser(ownerShopId, paging);
			listnewest = SanPhamService.getListDataNewestByUser(ownerShopId, paging);
		}else{
			// get all data
			listvip = SanPhamService.getListDataVip(paging);
			listnewest = SanPhamService.getListDataNewest(paging);
		}
		this.request.setAttribute("listvip", listvip);
		this.request.setAttribute("listnewest", listnewest);
		return ActionSupport.SUCCESS;
    }
		
	public String viewGroup() throws Exception {
		try{
			Long id = Long.valueOf(this.request.getParameter("shopping_group"));
			if(id != null && id > 0){
				ListSanPham listdata = null;
				Paging paging = new Paging();
				if(this.request.getParameter("shopping_search_page") != null){
					paging.setCurrent(Integer.valueOf(this.request.getParameter("shopping_search_page")));
				}
				paging.setPageSize(Integer.valueOf(ConfigUtil.getProperties().getProperty("guest.display.item.search", "10")));
				paging.setNumDisplay(Integer.valueOf(ConfigUtil.getProperties().getProperty("guest.display.numpage.item.search", "10")));
				listdata = SanPhamService.getListDataByGroup(id, paging);
				
				this.request.setAttribute("listdata", listdata);
				this.request.setAttribute("paging", paging);				
				return ActionSupport.SUCCESS;   
			}			
		}catch(Exception ex){
			return ActionSupport.ERROR;
		}
		return ActionSupport.ERROR;
    }
	
	public String viewType() throws Exception {	
		try{
			Long id = Long.valueOf(this.request.getParameter("shopping_type"));
			if(id != null && id > 0){
				ListSanPham listdata = null;
				Paging paging = new Paging();
				if(this.request.getParameter("shopping_search_page") != null){
					paging.setCurrent(Integer.valueOf(this.request.getParameter("shopping_search_page")));
				}
				paging.setPageSize(Integer.valueOf(ConfigUtil.getProperties().getProperty("guest.display.item.search", "10")));
				paging.setNumDisplay(Integer.valueOf(ConfigUtil.getProperties().getProperty("guest.display.numpage.item.search", "10")));
				listdata = SanPhamService.getListDataByType(id, paging);
				
				this.request.setAttribute("listdata", listdata);
				this.request.setAttribute("paging", paging);				
				return ActionSupport.SUCCESS;   
			}			
		}catch(Exception ex){
			return ActionSupport.ERROR;
		}
		return ActionSupport.ERROR;
    }

	public String viewDetail() throws Exception {	
		try{
			Long id = Long.valueOf(this.request.getParameter("productId"));
			SanPham data = SanPhamService.getData(id);
			if(data != null){				
				this.request.setAttribute("sanpham", data);				
				return ActionSupport.SUCCESS;
			}
		}catch(Exception ex){
			return ActionSupport.ERROR;
		}
		return ActionSupport.ERROR;
    }
	
	
	public  String viewSearch() throws Exception {	
		try{
			String keyword = this.request.getParameter("keyword");
			ListSanPham listdata = null;
			Paging paging = new Paging();
			if(this.request.getParameter("shopping_search_page") != null){
				paging.setCurrent(Integer.valueOf(this.request.getParameter("shopping_search_page")));
			}
			paging.setPageSize(Integer.valueOf(ConfigUtil.getProperties().getProperty("guest.display.item.search", "10")));
			paging.setNumDisplay(Integer.valueOf(ConfigUtil.getProperties().getProperty("guest.display.numpage.item.search", "10")));
			long ownerShopId = SedevUtil.getOwnerShopId(this.request);
			listdata = SanPhamService.search(ownerShopId, keyword, paging);
			
			this.request.setAttribute("listdata", listdata);
			this.request.setAttribute("paging", paging);
			this.request.setAttribute("keyword", keyword);
			return ActionSupport.SUCCESS;
		}catch(Exception ex){
			return "fail";
		}
    }
	
}
